package org.jeecg.common.util;

import org.apache.commons.codec.binary.Base64;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Img2Base64Util {

    /**
     * 本地图片转base64
     * @param imgFile 本地图片地址
     * @return base64字符串
     */
    public static String getImgStr(String imgFile) {
        //将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        if (null == imgFile || "".equals(imgFile)) {
            return "";
        }
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(Base64.encodeBase64(data));
    }


    /**
     * 网络图片转base64
     * @param filePath 网络图片地址
     * @return base64字符串
     */
    public static String netImg2Base64(String filePath){
        if (filePath == null || filePath.length() == 0) {
            return "";
        }
        byte[] data = null;
        InputStream in = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            URL url = new URL(filePath);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            in = connection.getInputStream();
            // out = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int len = 0;
            while ((len =  in.read(b))!=-1){
                out.write(b,0,len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if(in != null ){
                    in.close();
                }
            }catch (IOException e){
                e.getStackTrace();
            }
        }
        return new String(Base64.encodeBase64(out.toByteArray()));
    }

    public static void main(String[] args) throws IOException {
        String imgFile = "http://192.173.0.230:9000/busicenter/imagedata/avatar/1666790724700_MuMu20221022165954_1666790724711.png";//待处理的图片
        String imgbese=netImg2Base64(imgFile);
        String base64_file = "D:\\base64.txt";
        FileWriter fw = new FileWriter(base64_file);
        fw.write(imgbese);
        fw.flush();
        String img = "D:\\3322.jpg";
        generateImage(imgbese,img);
    }

    /**
     * 对字节数组字符串进行Base64解码并生成图片
     *
     * @param imgStr      图片数据
     * @param imgFilePath 保存图片全路径地址
     * @return
     */
    public static boolean generateImage(String imgStr, String imgFilePath) {
        if (imgStr == null) //图像数据为空
            return false;
        try {
            //Base64解码
            byte[] b = Base64.decodeBase64(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {//调整异常数据
                    b[i] += 256;
                }
            }
            //生成jpeg图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
